package cn.com.self.apis.controller;


import cn.com.self.apis.core.vo.ResultModel;
import cn.com.self.apis.member.entity.*;
import cn.com.self.apis.member.entity.vo.MemberInfoVO;
import cn.com.self.apis.member.mapper.MemberInfoMapper;
import cn.com.self.apis.member.service.IMemberInfoRelationService;
import cn.com.self.apis.member.service.IMemberInfoService;
import cn.com.self.apis.member.service.IMemberService;
import cn.com.self.apis.util.CopyUtils;
import cn.com.self.security.config.ImageCodeConfiguration;
import cn.com.selfsoft.smartcity.commons.http.DataItem;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author1 ssh
 * @author2
 * @date 2021-04-28
 */
@Controller
@Api(tags = "个人中心-个人信息")
public class PersonalInformationController {

    @Autowired
    private IMemberInfoService memberInfoService;
    @Autowired
    private IMemberService memberService;
    @Autowired
    private IMemberInfoRelationService memberRelationService;
    @Autowired
    private MemberInfoMapper memberInfoMapper;

    /**获取会员信息
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "获取会员信息")
    @RequestMapping(value = {"/memberInfo/load/{id}"}, method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity loadEscrowInfo(@PathVariable("id") String id, HttpServletRequest request) {
        ResponseEntity memberInfo = this.memberInfoService.loadMemberInfo(id);
        return memberInfo;

    }


    /**修改会员信息
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "修改会员信息")
    @RequestMapping(value = {"/memberInfo/update/{id}"}, method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity update(@PathVariable("id") String id, @RequestBody MemberInfoVO entity, HttpServletRequest request) {
        UMemberInfo memberInfo = this.memberInfoService.load(id);
        if(memberInfo == null){
            return ResponseEntity.status(HttpStatus.FAILED_DEPENDENCY).body(new cn.com.self.apis.core.vo.ResultModel<>(new DataItem(404,"不存在该账号信息！")));
        }
        CopyUtils.copyProperties(entity, memberInfo);
        memberInfo.setUpdateDate(new Date());
        UMemberInfo update = this.memberInfoService.update(memberInfo);
        if(entity.getMemberInfoRelations() != null && entity.getMemberInfoRelations().size() > 0){
            memberRelationService.deleteByMemberInfoId(update.getId());
            List<UMemberInfoRelation> memberInfoRelations = entity.getMemberInfoRelations();
            for(int i=0;i<memberInfoRelations.size();i++){
                UMemberInfoRelation memberInfoRelation = new UMemberInfoRelation();
                memberInfoRelation.setMemberInfoId(update.getId());
                memberInfoRelation.setType(memberInfoRelations.get(i).getType());
                memberInfoRelation.setVal(memberInfoRelations.get(i).getVal());
                memberRelationService.create(memberInfoRelation);
            }
        }
        /*UMember member = this.memberService.load(update.getMemberId().toString());
        UMember memberObject = new UMember();
        memberObject.setPassword(entity.getPassword());
        memberObject.setId(update.getMemberId());
        if(entity.getPassword() != null){
            CopyUtils.copyProperties(memberObject, member);
            UMember memberUpdate = this.memberService.update(memberObject);
        }*/
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(update)));

    }

    /**实名认证
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "实名认证")
    @RequestMapping(value = {"/memberInfo/authUpdate/{id}"}, method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity authUpdate(@PathVariable("id") String id, @RequestBody AuthMemberInfo entity, HttpServletRequest request) {
        UMemberInfo memberInfo = this.memberInfoService.load(id);
        if(memberInfo == null){
            return ResponseEntity.status(HttpStatus.OK).body(new cn.com.self.apis.core.vo.ResultModel<>(new DataItem(404,"不存在该账号信息！")));
        }
        UMemberInfo uMemberInfo = new UMemberInfo();
        uMemberInfo.setTelephone(entity.getPhone());
        uMemberInfo.setIdCarno(entity.getIdCarno());
        uMemberInfo.setRealName(entity.getRealName());
        uMemberInfo.setImgIdcarFront(entity.getImgIdcarFront());
        uMemberInfo.setImgIdcarBack(entity.getImgIdcarBack());
        CopyUtils.copyProperties(uMemberInfo, memberInfo);
        memberInfo.setUpdateDate(new Date());
        UMemberInfo update = this.memberInfoService.update(memberInfo);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(update)));

    }



    /**修改密码
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "修改密码")
    @RequestMapping(value = {"/memberInfo/updatePwd/{id}"}, method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity authUpdate(@PathVariable("id") String id, @RequestBody UMemberUpdate entity, HttpServletRequest request) {
        UMemberInfo load = this.memberInfoService.load(id);
        if(load == null){
            return ResponseEntity.status(HttpStatus.OK).body(new cn.com.self.apis.core.vo.ResultModel<>(404,"不存在该账号信息！"));
        }
        UMember uMember = memberService.load(load.getMemberId().toString());
        if(uMember != null && !uMember.getPassword().equals(entity.getOldPassword())){
            return ResponseEntity.status(HttpStatus.OK).body(new cn.com.self.apis.core.vo.ResultModel<>(404,"原账号密码不正确！"));
        }
        UMember uMemberNew = new UMember();
        uMemberNew.setPassword(entity.getNewPassword());
        CopyUtils.copyProperties(uMemberNew, uMember);
        uMember.setUpdateTime(new Date());
        UMember update = this.memberService.update(uMember);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>("修改成功！"));

    }

    @ApiOperation(value = "根据会员id查询会员")
    @GetMapping("/my/memberInfo/id/{memberId}")
    public ResponseEntity getMemberById(@PathVariable("memberId") String memberId) {
        Map<String, Object> map = new HashMap(1);
        map.put("member_id", memberId);
        List<UMemberInfo> uMemberInfos = memberInfoMapper.selectByMap(map);
        UMemberInfo uMemberInfo = uMemberInfos.get(0);
        if (uMemberInfo != null) {
            return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(uMemberInfo)));
        }
        return null;
    }





    /**忘记密码
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "忘记密码")
    @RequestMapping(value = {"/memberInfo/forgetPwd"}, method = RequestMethod.POST)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "mobilePhone", value = "手机号码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "newPwd", value = "新密码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "imgcode", value = "验证码", required = true, dataType = "String")
    })
    public ResponseEntity forgetPwd(@RequestParam(value="mobilePhone",required = true) String mobilePhone,@RequestParam(value="newPwd",required = true)String newPwd,@RequestParam(value="imgcode",required = true)String imgcode, HttpServletRequest request) throws Exception{
        ServletWebRequest servletWebRequest = new ServletWebRequest(request);
        new ImageCodeConfiguration().validateCode(servletWebRequest);
        UMember uMember = memberService.queryMemberByPhone(mobilePhone);
        if(uMember == null){
            return ResponseEntity.status(HttpStatus.OK).body(new cn.com.self.apis.core.vo.ResultModel<>(404,"不存在该账号信息！"));
        }
        UMember uMemberNew = new UMember();
        uMemberNew.setPassword(newPwd);
        CopyUtils.copyProperties(uMemberNew, uMember);
        uMember.setUpdateTime(new Date());
        UMember update = this.memberService.update(uMember);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>("修改成功！"));

    }
}

